﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;

namespace HtScrum.Service.Data
{
    public class EntityContractCollection<T> : IList<EntityContract<T>>
    {
        List<EntityContract<T>> internalList;

        public EntityContractCollection()
        {
            this.internalList = new List<EntityContract<T>>();
        }

        public int IndexOf(EntityContract<T> item)
        {
            return this.internalList.IndexOf(item);
        }

        public void Insert(int index, EntityContract<T> item)
        {
            item.State = eEntityState.New;
            this.internalList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            this.internalList[index].State = eEntityState.Delete;
        }

        public EntityContract<T> this[int index]
        {
            get
            {
                return this.internalList[index];
            }
            set
            {
                this.internalList[index] = value;
            }
        }

        public EntityContract<T>[] this[eEntityState state]
        {
            get
            {
                return this.internalList.Where(i => i.State == state).ToArray();
            }
        }

        public void Add(EntityContract<T> item)
        {
            if (item.State == eEntityState.None)
                item.State = eEntityState.New;

            this.internalList.Add(item);
        }

        public void Clear()
        {
            this.internalList.Clear();
        }

        public bool Contains(EntityContract<T> item)
        {
            return this.internalList.Contains(item);
        }

        public void CopyTo(EntityContract<T>[] array, int arrayIndex)
        {
            this.internalList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this.internalList.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(EntityContract<T> item)
        {
            item.State = eEntityState.Delete;
            return true;
        }

        public IEnumerator<EntityContract<T>> GetEnumerator()
        {
            return this.internalList.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.internalList.GetEnumerator();
        }
    }      
}
